package com.intern.hackathon.internhackathon;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "ParserError", "ParserError" })
public class DashBoard extends Activity {
	TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard);
		tv = (TextView) findViewById(R.id.textView1);
		tv.setVisibility(View.GONE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.item_1:
			Toast.makeText(getApplicationContext(), "Item 1 Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_2:
			Toast.makeText(getApplicationContext(), "Item 2 Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_3:
			Toast.makeText(getApplicationContext(), "Item 3 Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_4:
			Toast.makeText(getApplicationContext(), "Item 4 Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_5:
			Toast.makeText(getApplicationContext(), "Item 5 Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menu_settings:
			Toast.makeText(getApplicationContext(), "Menu Settings Clicked!!",
					Toast.LENGTH_SHORT).show();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onButtonClicker(View v) {

		switch (v.getId()) {
		case R.id.main_btn_notification:
			// Notification code will be called
			displayNotification();
			break;

		case R.id.main_btn_Gps:
			// fetch GPS will be called
			getGPSStats();
			break;

		default:
			break;
		}
	}

	@SuppressLint("ShowToast")
	private void getGPSStats() {
		// TODO Auto-generated method stub

		android.location.Location loc = null;
		Toast.makeText(getApplicationContext(), "GPS Identification Initiated",
				Toast.LENGTH_LONG);
		loc = getLocation(getApplicationContext());
		tv.setVisibility(View.VISIBLE);

		TextView tv2 = (TextView) findViewById(R.id.textView2);
		String text = "Latitude: " + Double.toString(loc.getLatitude())
				+ " Longitude: " + Double.toString(loc.getLongitude());
		text += "\nAltitude: " + loc.getAltitude() + "\nProvider: "
				+ loc.getProvider() + "\nAccuracy: " + loc.getAccuracy();
		tv2.setText(text);

	}

	
	public android.location.Location getLocation(Context ctx) {
		LocationManager lm = (LocationManager) ctx
				.getSystemService(Context.LOCATION_SERVICE);

		LocationListener locationListener = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "GPS Status Changed",
						Toast.LENGTH_LONG).show();
			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "GPS Provider Enabled "+provider,
						Toast.LENGTH_LONG).show();
			}

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "GPS Provider Disabled "+provider,
						Toast.LENGTH_LONG).show();
			}

			@Override
			public void onLocationChanged(android.location.Location location) {
				// TODO Auto-generated method stub
				// Trigger the event here!!!!
				Toast.makeText(getApplicationContext(), "GPS Location Changed",
						Toast.LENGTH_LONG).show();
			}
		};

		List<String> providers = lm.getProviders(true);
		/*
		 * Loop over the array backwards, and if you get an accurate location,
		 * then break out the loop
		 */
		android.location.Location l = null;

		for (int i = providers.size() - 1; i >= 0; i--) {

			l = lm.getLastKnownLocation(providers.get(i));
			if (l != null) {
				// String text = "Latitude: "+Double.toString(l.getLatitude()) +
				// " Longitude: "+Double.toString(l.getLongitude()) ;
				// Toast.makeText(getApplicationContext(), text,
				// Toast.LENGTH_LONG).show();
				break;
			} else {
				System.out.println("L is null!!");
			}
		}
		if (providers.size() == 0) {
			Toast.makeText(getApplicationContext(), "No results Found!!",
					Toast.LENGTH_LONG).show();
		}
		return l;
	}

	@SuppressLint("ParserError")
	private void displayNotification() {
		// TODO Auto-generated method stub
		int id = 0;

		// Refrence to notification Manager
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

		// Init the manager
		int icon = R.drawable.app_icon;
		CharSequence tickerText = "Sample text.. Hello!!";
		long when = System.currentTimeMillis();

		Notification notificationInst = new Notification(icon, tickerText, when);

		// define msg and pending intent
		Context context = getApplicationContext();
		CharSequence contentTitle = "Hackathon App";
		CharSequence contentText = "This is to notify that.. blah blah blah..";
		Intent notification = new Intent(this, DashBoard.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				notification, 0);

		notificationInst.setLatestEventInfo(context, contentTitle, contentText,
				pendingIntent);

		notificationManager.notify(id, notificationInst);

		Toast.makeText(getApplicationContext(),
				"Drag the notification bar to see the posted Notification!!",
				Toast.LENGTH_LONG).show();
	}

}
